<?php

###########################################################################
#   Copyright (C) 2012 CDNPAL LTD by Christopher Gu                       #
#   resources@cdnpal.com                                                  #
#                                                                         #
#   coded for Php 4.3/5.X and above                                       #
#   and AllowOverride all for .htaccess                                   #
#                                                                         #
#   This program is free software released                                #
#   under the GPL version 2 license:                                      #
#   http://www.gnu.org/licenses/gpl-2.0.htm                               #
#                                                                         #
#   This program is distributed WITHOUT ANY WARRANTY                      #
#   or FITNESS FOR A PARTICULAR PURPOSE.                                  #
#                                                                         #
#   DADF-SDK                                                              #
#                                                                         #
###########################################################################

class IncomingOfferTransformer extends IncomingTransformer {
	
	private $table_direction = 'Incoming';
	
	public $offer_response_id_list = array();
	
	public function __construct($request_xml, $schema_filename, $table_direction = 'Incoming') {
		
		$this->table_direction = $table_direction;
		
		$xmlservice = new XMLService($request_xml, $schema_filename);

		$response_list = $xmlservice->get_node_children($xmlservice->doc, 'response');
		
		foreach ($response_list as $response):

			$class_name 						= $this->table_direction .  'OfferResponse';
			$offer_response 					= new $class_name();
			
			$accept 							= $xmlservice->get_single_child($response, 'accept');
			$id		 							= $xmlservice->get_single_child($response, 'id');
			$offertype		 					= $xmlservice->get_single_child($response, 'offertype');
			$price		 						= $xmlservice->get_single_child($response, 'price');
			$cashdeal		 					= $xmlservice->get_single_child($response, 'cashdeal');
			$pausetrigger		 				= $xmlservice->get_single_child($response, 'pausetrigger');
			$resumetrigger		 				= $xmlservice->get_single_child($response, 'resumetrigger');
			$minutestoexpire		 			= $xmlservice->get_single_child($response, 'minutestoexpire');
			$message		 					= $xmlservice->get_single_child($response, 'message');
			$acceptid		 					= $xmlservice->get_single_child($response, 'acceptid');
			$partnerid		 					= $xmlservice->get_single_child($response, 'partnerid');
			$partnername		 				= $xmlservice->get_single_child($response, 'partnername');
			
			$offer_response->Accept				= $this->from_boolean($accept);
			$offer_response->ID					= $id;
			$offer_response->OfferType			= $offertype;
			$offer_response->Price				= $price;
			
			if ($cashdeal !== null):
				$offer_response->CashDeal		= $cashdeal;
			endif;
			
			if ($pausetrigger !== null):
				$offer_response->PauseTrigger	= $pausetrigger;
			endif;
			
			if ($resumetrigger !== null):
				$offer_response->ResumeTrigger	= $resumetrigger;
			endif;
			
			if ($minutestoexpire !== null):
				$offer_response->MinutesToExpire = $minutestoexpire;
			endif;
			
			$offer_response->Message			= $message;
			$offer_response->AcceptID			= $acceptid;
			$offer_response->PartnerID			= $partnerid;
			$offer_response->PartnerName		= $partnername;
			
			$offer_response->save();
			$column_name = $this->table_direction . 'OfferResponseID';
			$this->offer_response_id_list[] 	= $offer_response->$column_name;
			
		endforeach;
		
	}	
	
}

